/*
 * This file is part of the Try! Engine asset.
 *
 * Copyright 2010-2011 Emanuele Bertoldi. All rights reserved.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * You should have received a copy of the modified BSD License along with this
 * program. If not, see <http://www.opensource.org/licenses/bsd-license.php>
 */

#include "assetmodellist.h"

using namespace Try;

AssetModelList::AssetModelList(QObject* parent)
    :   QAbstractListModel(parent)
{
    QHash<int,QByteArray> rolenames;
    rolenames[AssetModelList::NameRole] = "name";
    rolenames[AssetModelList::IconRole] = "icon";
    rolenames[AssetModelList::LibraryRole] = "library";
    rolenames[AssetModelList::TypeRole] = "type";
    this->setRoleNames(rolenames);
}

AssetModelList::~AssetModelList()
{
}

int AssetModelList::rowCount(const QModelIndex& parent) const
{
    Q_UNUSED(parent)

    return m_assets.count();
}

QVariant AssetModelList::data(const QModelIndex& index, int role) const
{
    if (index.isValid())
    {
        int row = index.row();
        Asset* asset = m_assets.at(row);

        switch (role)
        {
        case Qt::DisplayRole: // AssetModelList::NameRole
            return asset->name();

        case Qt::DecorationRole: // AssetModelList::IconRole
            return asset->icon();

        case AssetModelList::LibraryRole:
            return asset->library();

        case AssetModelList::TypeRole:
            return asset->type();

        default:
            break;
        }
    }

    return QVariant();
}

Asset* AssetModelList::assetAt(int row) const
{
    if (row < m_assets.count())
        return m_assets.at(row);

    return 0;
}

Asset* AssetModelList::findAssetByName(const QString& name) const
{
    foreach (Asset* asset, m_assets)
        if (asset->name() == name)
            return asset;

    return 0;
}

bool AssetModelList::addAsset(Asset* asset)
{
    return this->insertAsset(m_assets.count(), asset);
}

bool AssetModelList::insertAsset(int row, Asset* asset)
{
    if (asset && !m_assets.contains(asset))
    {
        if (row < 0)
            row = qMax(0, m_assets.count()-row);

        this->beginInsertRows(QModelIndex(), row, row+1);

        m_assets.insert(row, asset);

        this->endInsertRows();

        return true;
    }

    return false;
}

bool AssetModelList::removeAsset(int row)
{
    if (row < 0)
        row = qMax(0, m_assets.count()-row);

    if (row < m_assets.count())
    {
        this->beginRemoveRows(QModelIndex(), row, row+1);

        Asset* asset = m_assets.takeAt(row);

        this->endRemoveRows();

        return true;
    }

    return false;
}

bool AssetModelList::removeAsset(Asset* asset)
{
    if (asset && m_assets.contains(asset))
        return this->removeAsset(m_assets.indexOf(asset));

    return false;
}
